﻿using Bussiness;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace tool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected bool CreateUsername(string username, string password, string email, bool sex, string nickname,int xukhoa, int money, int store)
        {
           //password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");
            bool ok = false;
            using (var db = new MemberShipBussiness())
            {
                if (db.CreateUsername("DanDanTang", username, password, email, "1", "MD5", sex))
                {
                    //TikIDServices service = new TikIDServices();
                    //string x = service.RegisterUser(username, nickname, password, email, 0, phone, "hoandd",
                    //                 ConfigurationManager.AppSettings["token"]);
                    //if (x == "1")
                    using (var db1 = new PlayerBussiness())
                    {

                        db1.RegisterUser(username, nickname, password, sex, money, xukhoa, 0);
                        db1.SendMailAndItemByUserName("Đá cường hóa", "Quà tặng đền bù ", username, 11025, store, 0, 0, 0, 0, 0, 0, 0, 0, true);
                        int[] tempIdRan = { 7015, 7016, 7017, 7018, 7019, 7020, 7021, 7022, 7023 };
                        var randString = new Random();
                        var tmpID = tempIdRan[randString.Next(0, tempIdRan.Length - 1)];
                        db1.SendMailAndItemByUserName("Vũ khí wow", "Quà tân thủ", username, tmpID, 1, 7, 0, 0, 0, 0, 0, 0, 0, true);
                        db1.SendMailAndItemByUserName("Nước kinh nghiệm 4", "Quà tân thủ ", username, 11904, 1, 30, 0, 0, 0, 0, 0, 0, 0, true);
                        db1.SendMailAndItemByUserName("Đá cường hóa", "Quà tân thủ ", username, 11025, 10, 30, 0, 0, 0, 0, 0, 0, 0, true);

                    }
                        ok = true;
                }
               
            }
            return ok;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //if()
                var xxx=new Db1DataContext();
                var lis = from n in xxx.Mem_Users
                          join m in xxx.Mem_UserInfos on n.UserId equals m.UserId
                          select new { n.UserName, m.Password, m.Email, m.UserSex };
                var a = new List<string>();
                var db2 = new db2DataContext();
                foreach (var item in lis)
                {
                    var user=db2.Sys_Users_Details.SingleOrDefault(c => c.UserName == item.UserName);
                    bool sex = item.UserSex.ToString() != "False";
                    if (user == null)
                    {
                        if (CreateUsername(item.UserName, item.Password, item.Email, sex, item.UserName,0,0,0)==false)
                        {
                            a.Add(item.UserName);
                        }
                    }
                    else
                    {
                        int xuKhoa = user.Total * 150;
                        var item1 = item;
                        var xuThuong = db2.Charge_Moneys.Where(c => c.UserName == item1.UserName);
                       int money = Enumerable.Sum(xuThuong, tien => tien.Money);

                      //  xuKhoa += tien2;
                        string nickname = user.NickName;
                        var stone = (xuKhoa+money) / 1000;

                        if (CreateUsername(item.UserName, item.Password, item.Email, sex, nickname, xuKhoa, money, stone) == false)
                        {
                            a.Add(item.UserName);
                        }
                    }                 
                   
                }
                MessageBox.Show("complate");

                listBox1.DataSource = a;

        }
    }
}
